package reduce.fz.com.reduce.fragment;


import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.StoryCard;
import reduce.fz.com.reduce.bean.StoryReply;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ScreenUtils;

/**
 * 故事卡片推荐流
 */
public class StoryRecommendFragment extends BaseListFragment<StoryRecommendFragment.StoryRecommendViewHolder, StoryCard> {


    public StoryRecommendFragment() {
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        //默认返回列表
        return null;
    }

    public interface StoryRecommendListener {
        public void onRecommendClick(StoryCard card);
    }


    @Override
    protected StoryRecommendViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_story_recommend_card, parent, false);
        StoryRecommendViewHolder holder = new StoryRecommendViewHolder(view, new StoryRecommendListener() {
            @Override
            public void onRecommendClick(StoryCard card) {
                Intent intent = new Intent(context, null);
                intent.putExtra("storyid", card.storyid);
                startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    protected StoryRecommendViewHolder createfootViewHolder(View view) {
        return new StoryRecommendViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<StoryCard> datas, Pagination pagination) {
        if (datas != null && !datas.isEmpty())
            pagination.setStartNum(datas.get(datas.size() - 1).storyid);
        String url = ApiUtil.getApi(context, R.array.api_story_recommends);
        return url;
    }

    @Override
    protected int getPageType() {
        return Pagination.TYPE_START_NUM;
    }


    public static class StoryRecommendViewHolder extends BaseRecycleViewHolder<StoryCard> {
        private TextView mTvTitle;
        private NetworkImageView recommend1;
        private NetworkImageView recommend2;
        private NetworkImageView recommend3;
        private LinearLayout mllrecommends;
        private StoryRecommendListener storyRecommendListener;
        private View itemView;

        private int paddingLeft = context.getResources().getDimensionPixelSize(R.dimen.abc_action_bar1);
        private int paddingRight = context.getResources().getDimensionPixelSize(R.dimen.abc_action_bar1);
        private int margincenter = context.getResources().getDimensionPixelSize(R.dimen.abc_action_bar1);

        public StoryRecommendViewHolder(View itemView, StoryRecommendListener listener) {
            this(itemView);
            this.itemView = itemView;
            this.storyRecommendListener = listener;
        }

        public StoryRecommendViewHolder(View itemView) {
            super(itemView);
            mTvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            recommend1 = (NetworkImageView) itemView.findViewById(R.id.recommend1);
            recommend2 = (NetworkImageView) itemView.findViewById(R.id.recommend2);
            recommend3 = (NetworkImageView) itemView.findViewById(R.id.recommend3);
            mllrecommends = (LinearLayout) itemView.findViewById(R.id.ll_recommends);
            int height = calHeight();
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
            mllrecommends.setPadding(paddingLeft, 0, paddingRight, 0);

        }

        public int calHeight() {
            int width = ScreenUtils.getScreenW();
            int length = width - 2 * margincenter - paddingLeft - margincenter;
            length = length / 3;
            return length;
        }

        @Override
        public void renderView(final StoryCard data, int position) {
            mTvTitle.setText(data.title);
            if (data.picCards.size() >= 1)
                recommend1.setImageUrl(data.picCards.get(0).cardurl, HttpUtils.getImageLoader());
            if (data.picCards.size() >= 2)
                recommend1.setImageUrl(data.picCards.get(1).cardurl, HttpUtils.getImageLoader());
            if (data.picCards.size() >= 3)
                recommend1.setImageUrl(data.picCards.get(2).cardurl, HttpUtils.getImageLoader());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    storyRecommendListener.onRecommendClick(data);

                }
            });

        }
    }


}
